﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace FindDuplicateFile
{
    /// <summary>
    /// Interaction logic for ExplorerFS.xaml
    /// </summary>
    public partial class ExplorerFS : UserControl, INotifyPropertyChanged
    {
        public ExplorerFS()
        {
            Initialize(null);
        }

        public ExplorerFS(FSDirectoryNode _ExplorerCurrentDir)
        {
            Initialize(_ExplorerCurrentDir);
        }

        private void Initialize(FSDirectoryNode _ExplorerCurrentDir)
        {
            this.DataContext = this;
            InitializeComponent();

            this.gridExplorerDirectories.SelectAction = (grid) =>
            {
                if (grid.CurrentDirectory != null)
                {
                    ChangeCurrentDir(grid.CurrentDirectory);
                }
            };

            CurrentDir = _ExplorerCurrentDir;
        }

        public FSDirectoryNode CurrentDir
        {
            get 
            {
                return m_CurrentDir; 
            }
            set 
            {
                if (value != m_CurrentDir) 
                { 
                    ChangeCurrentDir(value); 
                } 
            }
        }
        private FSDirectoryNode m_CurrentDir;

        private void btnExplorerPopDir_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentDir == null) return;
            if (CurrentDir.Parent == null) return;

            ChangeCurrentDir(CurrentDir.Parent);
        }

        private void btnExplorerCurrentDir_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentDir == null) return;

            ChangeCurrentDir(CurrentDir);
        }

        public FSDirectoryNode ChangeCurrentDir(FSDirectoryNode newDir)
        {
            FSDirectoryNode previousDir = CurrentDir;

            m_CurrentDir = newDir;
            NotifyPropertyChanged("CurrentDir");

            if (CurrentDir == null)
            {
                this.gridExplorerDirectories.Directories = null;
                this.gridExplorerFiles.Files = null;
            }
            else
            {
                this.gridExplorerDirectories.Directories = CurrentDir.Directories;
                this.gridExplorerFiles.Files = CurrentDir.Files;
            }

            return previousDir; 
        }

        #region Fucking/Garbage for : INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion Fucking/Garbage for : INotifyPropertyChanged
    }
}
